import { rcaNumberFormat } from './common.js'
import { dateFormat } from '@/util/date';
import { commonTableOption } from './common.js'

function noDiff (val) {
  return val === undefined || val === 0
}
export const startDate = dateFormat(new Date(), 'yyyy-MM-') + '01';
export const endDate = dateFormat(new Date(), 'yyyy-MM-dd');
export const tableOption = {
  ...commonTableOption,
  page: false,
  selection: true,
  showSummary: true,
  height: '400px',
  // selectable: (row, index) => {
  //   return (
  //     row.confirmStatus === '0'
  //     // noDiff(row.lineGroup1_title1_diffMoney) &&
  //     // noDiff(row.lineGroup1_title2_diffMoney) &&
  //     // noDiff(row.lineGroup1_title3_diffMoney) &&
  //     // noDiff(row.lineGroup2_title2_diffMoney) &&
  //     // noDiff(row.lineGroup2_title3_diffMoney)
  //   );
  // },
  column: [{
    type: 'date',
    width: "100px",
    label: '日期',
    prop: 'tradeDate',
    searchSpan: 12,
    searchRange: true,
    search: true,

    searchValue: [startDate, endDate],      // 这是我获取的默认的显示日期，我这里是['2020-08-04', '2020-09-22']
    format: 'yyyy-MM-dd',                  // 这是组件展示的日期格式
    valueFormat: 'yyyy-MM-dd',        // 这是组件value值的格式
  }, {
    label: '支付宝、微信、银联交易统计表',
    children: [{
      label: 'TVM电子收入（一二五九十四号线）',
      children: [{
        label: '建行',
        children: [{
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '交易金额',
          prop: 'lineGroup1_title1_money',
        }, {
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '手续费',
          prop: 'lineGroup1_title1_serviceCharge'
        }, {
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '银行存款',
          prop: 'lineGroup1_title1_bankMoney'
        }]
      }, {
        label: '支付宝',
        children: [{
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '交易金额',
          prop: 'lineGroup1_title2_money'
        }, {
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '手续费',
          prop: 'lineGroup1_title2_serviceCharge'
        }, {
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '银行存款',
          prop: 'lineGroup1_title2_bankMoney'
        }]
      }, {
        label: '微信',
        children: [{
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '交易金额',
          prop: 'lineGroup1_title3_money'
        }, {
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '手续费',
          prop: 'lineGroup1_title3_serviceCharge'
        }, {
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '银行存款',
          prop: 'lineGroup1_title3_bankMoney'
        }]
      }]
    }, {
      label: 'TVM电子收入（三四六号线）',
      children: [{
        label: '支付宝',
        children: [{
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '交易金额',
          prop: 'lineGroup2_title2_money'
        }, {
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '手续费',
          prop: 'lineGroup2_title2_serviceCharge'
        }, {
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '银行存款',
          prop: 'lineGroup2_title2_bankMoney'
        }]
      }, {
        label: '微信',
        children: [{
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '交易金额',
          prop: 'lineGroup2_title3_money'
        }, {
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '手续费',
          prop: 'lineGroup2_title3_serviceCharge'
        }, {
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '银行存款',
          prop: 'lineGroup2_title3_bankMoney'
        }]
      }]
    }, {
      label: '差异（交易-银行）',
      children: [{
        label: '一二五九十四号线',
        children: [{
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '建行',
          prop: 'lineGroup1_title1_diffMoney',
          slot: true
        }, {
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '支付宝',
          prop: 'lineGroup1_title2_diffMoney',
          slot: true
        }, {
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '微信',
          prop: 'lineGroup1_title3_diffMoney',
          slot: true
        }]
      }, {
        label: '三四六号线',
        children: [{
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '支付宝',
          prop: 'lineGroup2_title2_diffMoney',
          slot: true
        }, {
          type: 'input',
          showSummary: true,
          precision: 2,
          formatter: (row, value, label, column) => {
            return rcaNumberFormat(value, true)
          },
          label: '微信',
          prop: 'lineGroup2_title3_diffMoney',
          slot: true
        }]
      }]
    }]
  }, {
    type: 'select',
    label: '确认状态',
    prop: 'confirmStatus',
    search: true,
    dicData: [
      {
        label: '已确认',
        value: '1'
      }, {
        label: '未确认',
        value: '0'
      }
    ]
  }]
}


function uniqueArray (arr) {
  return Array.from(new Set(arr))
}
function sum (arr) {
  var s = 0;
  for (var i = arr.length - 1; i >= 0; i--) {
    s += arr[i];
  }
  return s;
}

export const tableDataFormat = (data) => {
  // 所有日期
  const dateArr = uniqueArray(data.map(d => d.tradeDate))
  // 所有线路
  const lineGroupArr = ['1', '2']
  // 所有title数据
  const titleArr = ['1', '2', '3']
  const dateMap = {}
  const lineGroupSum = dateArr.map(date => {
    // 当前日期所有数据
    const temp = data.filter(d => d.tradeDate === date)
    dateMap[date] = {}
    lineGroupArr.forEach(lineGroup => {
      dateMap[date]['lineGroup' + lineGroup] = {}
      // 当前日期下当前线路的数据
      const tempLineGroupData = temp.filter(d => d.lineGroup === lineGroup)
      titleArr.forEach(title => {
        // 当前日期下当前线路当前title的数据
        dateMap[date]['lineGroup' + lineGroup]['title' + title] = tempLineGroupData.find(d => d.title === title) || {};
      })
    })
    const res = {
      id: date,
      tradeDate: date,
      ids: temp.map(t => t.id),

      lineGroup1_title1_diffMoney: dateMap[date]['lineGroup1']['title1'].diffMoney,
      lineGroup1_title2_diffMoney: dateMap[date]['lineGroup1']['title2'].diffMoney,
      lineGroup1_title3_diffMoney: dateMap[date]['lineGroup1']['title3'].diffMoney,

      lineGroup2_title2_diffMoney: dateMap[date]['lineGroup2']['title2'].diffMoney,
      lineGroup2_title3_diffMoney: dateMap[date]['lineGroup2']['title3'].diffMoney,

      lineGroup1_title1_count: dateMap[date]['lineGroup1']['title1'].count,
      lineGroup1_title1_money: dateMap[date]['lineGroup1']['title1'].money,
      lineGroup1_title1_serviceCharge: dateMap[date]['lineGroup1']['title1'].serviceCharge,
      lineGroup1_title1_bankMoney: dateMap[date]['lineGroup1']['title1'].bankMoney,

      lineGroup1_title2_count: dateMap[date]['lineGroup1']['title2'].count,
      lineGroup1_title2_money: dateMap[date]['lineGroup1']['title2'].money,
      lineGroup1_title2_serviceCharge: dateMap[date]['lineGroup1']['title2'].serviceCharge,
      lineGroup1_title2_bankMoney: dateMap[date]['lineGroup1']['title2'].bankMoney,

      lineGroup1_title3_count: dateMap[date]['lineGroup1']['title3'].count,
      lineGroup1_title3_money: dateMap[date]['lineGroup1']['title3'].money,
      lineGroup1_title3_serviceCharge: dateMap[date]['lineGroup1']['title3'].serviceCharge,
      lineGroup1_title3_bankMoney: dateMap[date]['lineGroup1']['title3'].bankMoney,

      lineGroup2_title2_count: dateMap[date]['lineGroup2']['title2'].count,
      lineGroup2_title2_money: dateMap[date]['lineGroup2']['title2'].money,
      lineGroup2_title2_serviceCharge: dateMap[date]['lineGroup2']['title2'].serviceCharge,
      lineGroup2_title2_bankMoney: dateMap[date]['lineGroup2']['title2'].bankMoney,

      lineGroup2_title3_count: dateMap[date]['lineGroup2']['title3'].count,
      lineGroup2_title3_money: dateMap[date]['lineGroup2']['title3'].money,
      lineGroup2_title3_serviceCharge: dateMap[date]['lineGroup2']['title3'].serviceCharge,
      lineGroup2_title3_bankMoney: dateMap[date]['lineGroup2']['title3'].bankMoney,

      confirmStatus: uniqueArray(temp.map(t => t.confirmStatus))[0],


      lineGroup1_title1_diffUrl: dateMap[date]['lineGroup1']['title1'].diffBankUrl,
      lineGroup1_title2_diffUrl: dateMap[date]['lineGroup1']['title2'].diffAlipayUrl,
      lineGroup1_title3_diffUrl: dateMap[date]['lineGroup1']['title3'].diffWechartUrl,

      lineGroup2_title2_diffUrl: dateMap[date]['lineGroup2']['title2'].diffAlipayUrl,
      lineGroup2_title3_diffUrl: dateMap[date]['lineGroup2']['title3'].diffWechartUrl,
    }
    return res
  })
  return lineGroupSum
}
